fix(jax): defensive pytree dedup in imaging/interferometer analyses#468
Merged
Conversation
`AnalysisImaging._register_fit_imaging_pytrees` and the interferometer counterpart re-registered ``DatasetModel`` on every ``fit_from`` call even though autoarray's ``_pytree_registered_classes`` is supposed to make ``register_instance_pytree`` idempotent. The breakdown surfaced in the 2026.5.29.2 release on `multi/start_here.py`: ValueError: Duplicate custom PyTreeDef type registration for <class 'autoarray.dataset.dataset_model.DatasetModel'>. Root cause: ``autofit.jax.pytrees.register_model`` walks the model and calls ``register_pytree_node`` for any class it finds (including ``DatasetModel`` when present in the model). It dedupes via its own ``_REGISTERED_INSTANCE_CLASSES`` set, **independent** of autoarray's ``_pytree_registered_classes``. Subsequent ``register_instance_pytree(DatasetModel)`` doesn't find it in autoarray's set, asks JAX to register again, JAX rejects. Mirrors the existing defense in ``autogalaxy/ellipse/model/analysis.py``: * Module-level ``_FIT_*_PYTREES_REGISTERED`` flag — skip re-entry. * Cross-populate autoarray's set from autofit's so ``register_instance_pytree`` short-circuits on classes autofit already handled. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ports the existing defensive registration pattern from `autogalaxy/ellipse/model/analysis.py` to `imaging` and `interferometer` analyses. Without this, `AnalysisImaging.fit_from` re-registers `DatasetModel` with JAX every call, and crashes when a prior code path (PyAutoFit's pytree walker on a graphical / multi-dataset model) has already registered `DatasetModel` via an independent dedup set.
Repro
Discovered on `multi/start_here.py` in autogalaxy_workspace during the 2026.5.29.2 release run:
```
File "autogalaxy/imaging/model/analysis.py", line 152, in fit_from
self._register_fit_imaging_pytrees()
File "autogalaxy/imaging/model/analysis.py", line 221, in _register_fit_imaging_pytrees
register_instance_pytree(DatasetModel)
File "autoarray/abstract_ndarray.py", line 136, in register_instance_pytree
register_pytree_node(cls, flatten, unflatten)
ValueError: Duplicate custom PyTreeDef type registration for <class 'autoarray.dataset.dataset_model.DatasetModel'>.
```
Fix
For both `imaging/model/analysis.py` and `interferometer/model/analysis.py`:
Mirrors the defense in `autogalaxy/ellipse/model/analysis.py` (which solved the same problem for ellipse fits).
Paired with
Test plan
🤖 Generated with Claude Code